var cm=multiCheckModel_create();

var gridColumns=[new Ext.grid.RowNumberer(), cm,{
	header:'公式编号',
	align : 'center',
	dataIndex: "formulaId",
	id : 'messageId',
	menuDisabled:true,
	hidden :true
}, {
    header: "公式名称",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 150,
    id : "formulaName",
    dataIndex: "formulaName"
}, {
    header: "重 量",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 100,
    dataIndex: "weight"
}, {
    header: "颜 色",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 150,
    dataIndex: "colour"
}, {
    header: "净 度",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 150,
    dataIndex: "purity"
}, {
    header: "证书",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 150,
    dataIndex: "cerificate"
}, {
    header: "价格比例",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 100,
    dataIndex: "rate"
}, {
    header: "优先级 ",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 100,
    dataIndex: "importance"
}, {
    header: "操 作",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 100,
    dataIndex: "data3",
	renderer:function(){
		return '<span style="cursor:pointer;color:blue;">修改</span>'
	}
}]
function getFieldsFromColumns(colums){
	var fs=new Array();
	Ext.each(colums,function(col){
		fs.push(col.dataIndex);
	})
	return fs;
}

var gridModel=new Ext.grid.ColumnModel({
  defaultSortable: true,
  defaultWidth: 85,
  columns:gridColumns
})
var jsonReader=new Ext.data.JsonReader({
	
})
var gridStore=new Ext.data.JsonStore({
	proxy: new Ext.data.HttpProxy({
       url : 'queryFormula.action',
        failure: function(){
            Ext.MessageBox.alert("错误", "数据源获取失败!");
        }
    }),
	totalProperty: 'obj.totalSize', 
	successProperty :'success',
	root:'obj.returnBolist',
	messageProperty:'msg',
	fields:getFieldsFromColumns(gridColumns)
});


var gridConfig = {
	store : gridStore,
	cm : gridModel,
//	fbar : gridFootBt,
	sm : cm,
	animCollapse : true,
	frame : true,
	bbar : new Ext.PagingToolbar({
		pageSize : 15,
		store : gridStore,
		displayInfo : true,
		displyMsg : '显示第{0}条到第{1}条记录，一共{2}条',
		emptyMsg : "没有记录",
		items : [{
			text : '删除',
			style :'float:left',
			handler : function(bt, e) {
			var grid = bt.ownerCt.ownerCt;
			var cm = grid.getSelectionModel();
			var rds = cm.getSelections();
			Ext.Msg.confirm("提示", '你确信要执行删除操作？', function(at, mg) {
				if (at == 'yes') {
					var formulaIds = null;
					Ext.each(rds, function(rd) {
						formulaIds = (formulaIds != null)?(formulaIds + ";" + rd.get('formulaId')): rd.get('formulaId');
					})
//					alert('delet formula :' + formulaIds);
					var con = new connection({
						url : 'deleteFormula.action',
						params : {
							json : formulaIds
						},
						success : function(result, response) {
							searchFormulaInfo();
							var alt = result.success ? '成功' : '失败';
							Ext.Msg.alert(alt, result.msg);
						},
						failure : function(result, response) {
							searchFormulaInfo();
							var alt = result.success ? '成功' : '失败';
							Ext.Msg.alert(alt, result.msg);
						}
					});
					con.submit();
				}
			});
		}
		}]
	}),
	listeners : {
		render : function initGrid(grid) {
			searchFormulaInfo();
		},
		cellclick : function cellClick(grid, row, col, e) {
			var cm = grid.getColumnModel();
			var header = cm.getColumnHeader(col);
			if (header ==  '操 作') {
				var record = grid.getStore().getAt(row);
				initFormData(record);
			}
		}
	}
}

var formulaGrid = new Ext.grid.GridPanel(gridConfig);

var detailPanel=new Ext.FormPanel({
	layout:'column',
	xtype : 'form',
	frame:true,
	id : 'addFormulaForm',
	regetion:'north',
	defaults:{
		anchor: '80%',
		columnWidth:0.13,
		layout:'form',
		labelWidth:40,
		defaults:{
			anchor: '90%',
			xtype:'multiselect',
			editable : false,
			mode:"local",
			triggerAction : 'all',
			displayField : 'text',
			valueField : 'value'
		}
	},
	items:[{
		columnWidth:0.2,
		items:[{	
			xtype:'panel',
			layout:'column',
			defaults:{
				xtype:'panel',
				layout:'form'
			},
			items:[{
				columnWidth:0.58,
				items:[{
					anchor: '98%',
					fieldLabel:'重量',
					name : 'weightFrom',
					allowBlank:true,
					id : 'weightFrom_id',
					xtype:'numberfield'
				}]
			},{
				columnWidth:0.42,
				labelWidth:12,
				items:[{
					anchor: '98%',
					labelSeparator:'-',
					fieldLabel:'-',
					name : 'weightTo',
					id : 'weightTo_id',
					allowBlank:true,
					xtype:'numberfield'
				}]
			}]
		},{
			fieldLabel:'名称',
			name : 'formulaName',
			id:'formulaName_id',
			xtype:'textfield',
			allowBlank:false
		}]
	},{
		items:[{
			fieldLabel:'颜色',
			name:'colour',
			id:'colour_id',
			store:colorStore
		},{
			fieldLabel:'抛光',
			name : 'buffing',
			id:'buffing_id',
			store:polishStore
		}]
	},{
		labelWidth:50,
		items:[{
			fieldLabel:'对称性',
			id:'symmetry_id',
			name:'symmetry',
			store:synStore
		},{
			fieldLabel:'净度',
			name:'purity',
			id:'purity_id',
			store:netStore
		}]
	},{	
		items:[{
			fieldLabel:'切工',
			name:'cutter',
			id:'cutter_id',
			store:catTypeStore
		},{
			fieldLabel:'荧光',
			name:'fluorescence',
			id:'fluorescence_id',
			store:lightStore
		}]
	},{labelWidth:50,
		items:[{
			fieldLabel:'证书',
			name:'cerificate',
			id:'cerificate_id',
			store:creditStore
		},{
			fieldLabel:'优先级',
			xtype:'combo',
			name:'importance',
			id:'importance_id',
			store:importStore
		}]
	},{
		labelWidth:60,
		items:[{
			fieldLabel:'价格比例',
			name:'rate',
			id:'rate_id',
			xtype : 'textfield'
		//  store:creditStore
		},{
			fieldLabel:'公式种类',
			xtype:'combo',
			name:'state',
			id:'state_id',
			store:formulaTypeStore
		}]
	},{
		items:[{
			xtype:'button',
			text:'保存',
			id:'searchBt',
			anchor: '50%',
			handler: function(bt,e){
				var form = bt.findParentByType('form');
				var tag = form.getForm().isValid();
				if (tag) {
					saveOrUpdateFormula(detailPanel, e, 'save');
				}
			}
		},{
			xtype:'button',
			text:'重置',
			id:'resetBt',
			anchor: '50%'
		}]
	}]
});

function initFormData(record) {
	var con = new connection({
		url : 'findFormulaByFormulaId.action',
		params : {
			json : record.get("formulaId")
		},
		success : function(rep, opt) {
			if (rep.success) {
				if (rep.obj.rate < 1) {
					rep.obj.rate = ""
				}
				if (rep.obj.importance < 1) {
					rep.obj.importance = ""
				}
				if (rep.obj.state < 1) {
					rep.obj.state = ""
				}
				var win = showFormulaUpdateWindow();
				panel_initData(win, null, rep.obj)
			} else {
				Ext.Msg.alert('错误 ', "消息(" + rd.get('name') + ")加载失败!");
			}
		}
	})
	con.submit();
}

Ext.onReady(function() {
	new Ext.Viewport({
		layout : 'border',
		items : [{
			region : 'north',
			split : true,
			border : false,
			height : 65,
			collapseMode : 'mini',
			layout : 'fit',
			items : [detailPanel]
		}, {
			region : 'center',
			split : true,
			border : false,
			layout : 'fit',
			items : [formulaGrid]
		}]
	});
})
	
  